/**
 * 
 */
package sailing.ui.treecell;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.EventObject;

import javax.swing.JButton;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultTreeCellEditor;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

import cjlite.logger.Logger;

/**
 * @author kevin
 * 
 */
public class SubjectTreeCellEditor extends DefaultTreeCellEditor {

	private static Logger logger = Logger.thisClass();

	public SubjectTreeCellEditor(JTree tree, DefaultTreeCellRenderer renderer) {
		super(tree, renderer);
	}

	private Component isEditingComponent;

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.tree.DefaultTreeCellEditor#getTreeCellEditorComponent(javax.swing.JTree, java.lang.Object,
	 * boolean, boolean, boolean, int)
	 */
	@Override
	public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded,
			boolean leaf, int row) {
		// TODO Auto-generated method stub

		SubjectTreeCell edit = new SubjectTreeCell(value, isSelected, expanded, leaf, row, true);
		edit.doLayout();
		edit.addEditBtnListener(editAl);
		edit.addDeleteBtnListener(deleteAl);
		isEditingComponent = edit;
		edit.setName("TreeCell----Renderer----Component");
		return edit;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.tree.DefaultTreeCellEditor#shouldSelectCell(java.util.EventObject)
	 */
	@Override
	public boolean shouldSelectCell(EventObject anEvent) {

		if (!(anEvent instanceof MouseEvent)) {
			return false;
		}

		isEditingComponent.doLayout();
		
		Point componentPoint = SwingUtilities.convertPoint(tree, new Point(((MouseEvent) anEvent).getX(),
				((MouseEvent) anEvent).getY()), isEditingComponent);

		logger.trace("componentPoint=== {0}", componentPoint.toString());
		Component activeComponent = SwingUtilities.getDeepestComponentAt(isEditingComponent, componentPoint.x,
				componentPoint.y);
		logger.trace("activeComponent=== {0}", activeComponent.toString());
		if (activeComponent instanceof JButton) {
			// JButton btn=(JButton) activeComponent;
			// btn.doClick();
			return true;
		}

		return false;
	}

	private ActionListener editAl;

	public void addEditBtnListener(ActionListener editAl) {
		this.editAl = editAl;
	}

	private ActionListener deleteAl;

	public void addDeleteBtnListener(ActionListener deleteAl) {
		this.deleteAl = deleteAl;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.tree.DefaultTreeCellEditor#isCellEditable(java.util.EventObject)
	 */
	@Override
	public boolean isCellEditable(EventObject event) {
		if (event != null) {
			if (event.getSource() instanceof JTree) {
				setTree((JTree) event.getSource());
				if (event instanceof MouseEvent) {
					TreePath path = tree.getPathForLocation(((MouseEvent) event).getX(), ((MouseEvent) event).getY());
					//editable = (lastPath != null && path != null && lastPath.equals(path));
					if (path != null) {
						lastRow = tree.getRowForPath(path);
						Object value = path.getLastPathComponent();
						boolean isSelected = tree.isRowSelected(lastRow);
						boolean expanded = tree.isExpanded(path);
						TreeModel treeModel = tree.getModel();
						boolean leaf = treeModel.isLeaf(value);
						determineOffset(tree, value, isSelected, expanded, leaf, lastRow);
					}
					
					Component activeComp=tree.getComponentAt(((MouseEvent) event).getX(), ((MouseEvent) event).getY());
					logger.trace("Active component::: {0}", activeComp.toString());

				}
			}
		}
		return true;
	}

	
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.tree.DefaultTreeCellEditor#stopCellEditing()
	 */
	@Override
	public boolean stopCellEditing() {
		this.isEditingComponent = null;
		return super.stopCellEditing();
	}

	/* (non-Javadoc)
	 * @see javax.swing.tree.DefaultTreeCellEditor#cancelCellEditing()
	 */
	@Override
	public void cancelCellEditing() {
		this.isEditingComponent = null;
		super.cancelCellEditing();
	}
	
    /**
     * Returning true indicates the row under the mouse should be toggled
     * based on the event. This is invoked after checkForClickInExpandControl,
     * implying the location is not in the expand (toggle) control
     */
    protected boolean isToggleEvent(MouseEvent event) {
        if(!SwingUtilities.isLeftMouseButton(event)) {
            return false;
        }
        int           clickCount = tree.getToggleClickCount();

        if(clickCount <= 0) {
            return false;
        }
        return ((event.getClickCount() % clickCount) == 0);
    }



}
